package com.bdqn.ThymeleafJPA.controller;


import com.bdqn.ThymeleafJPA.entity.User;
import com.bdqn.ThymeleafJPA.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
public class ExampleController {
    @Resource
    private IUserService userService;

    @GetMapping("/hello/{id}")
    public String getUser(Model model, @PathVariable Integer id){
        User user= userService.getUserById(id);
        model.addAttribute("user",user);
        return "demo/hello";
    }

    @GetMapping("/string")
    public String getString(Model model, HttpServletRequest request){
//        model.addAttribute("name","ktjiaoyu");
        model.addAttribute("userName","ktjiaoyu");
        request.setAttribute("test","request");
        request.getSession().setAttribute("test","session");
        model.addAttribute("date",new Date());
        return "demo/string";
    }

    @GetMapping("/toInline")
    public String toInline(Model model){
        model.addAttribute("userName","ktjiaoyu");
        return "demo/inline";
    }

    @GetMapping("/list")
    public String getList(Model model){
//        model.addAttribute("user",userService.getUserById(1));
        model.addAttribute("users",userService.selectUserList());
        return "demo/list";
    }

    @GetMapping("/if/{flag}/{sex}")
    public String getIf(Model model,@PathVariable String flag, @PathVariable String sex){
        model.addAttribute("flag",flag);
        model.addAttribute("sex",sex);
        return "demo/if";
    }

    @GetMapping("/index")
    public String index(){
        return "demo/index";
    }

    @GetMapping("/fragment")
    public String fragment(){
        return "demo/fragment";
    }

    @GetMapping("/test")
    public String test(){
        return "demo/test";
    }

    @RequestMapping("/dologin")
    public String dologin(String username){
        System.out.println(username);
        if(username==null){
            return "demo/login";
        }
        return "demo/index";
    }
}
